package com.newbee.fragement.multchat;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hhu.newbee.R;
import com.newbee.activity.LoginActivity;
import com.newbee.activity.MultChatChooseFriend;
import com.newbee.app.OnRefrushActivity;
import com.newbee.multchat.MultChatActivity;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceKey;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.sound.Sound;
import com.newbee.sqlite.DBHelper;

public class MultChatResultFragement extends Fragment implements
		OnClickListener, OnRefrushActivity
{

	private TextView resultTextView;

	private ProgressDialog mpd;
	private String tag;

	private String mRoomName;

	private View mBack;
	private ImageButton mSendMail;

	private View Root;
	private DBHelper mDbHelper;

	private String qunzuName;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{

		Root = inflater.inflate(R.layout.fragement_multchatresult, container,
				false);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String date = sdf.format(new Date()).toString();

		EditText pStart = (EditText) Root.findViewById(R.id.perpsion_start);
		EditText dtime = (EditText) Root.findViewById(R.id.date_time);

		SharedPreferences sp = this.getActivity().getSharedPreferences(
				LoginActivity.SP_USER_INFO, Activity.MODE_MULTI_PROCESS);
		String userName = sp.getString(LoginActivity.SP_KEY_USERNAME, "error");
		pStart.setText(userName);
		dtime.setText(date);

		mDbHelper = new DBHelper(this.getActivity());
		initDatas();
		initLayout();
		return Root;
	}

	public void showProgressDlg()
	{
		this.mpd.show();

	}

	private void initDatas()
	{
		Bundle bundle = this.getActivity().getIntent().getExtras();
		mRoomName = bundle.getString(MultChatFragement.CHAT_ROOM_MAME);
		qunzuName = bundle.getString(MultChatChooseFriend.QZ_NAME);
	}

	private void initLayout()
	{
		this.mpd = new ProgressDialog(this.getActivity(),
				ProgressDialog.STYLE_SPINNER);
		this.mpd.setTitle("请稍等");
		this.mpd.setMessage("正在拼命为您转换     ╥﹏╥...");
		mpd.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
		resultTextView = (TextView) Root.findViewById(R.id.multresult_text);
		Root.findViewById(R.id.multchat_result_back).setOnClickListener(this);
		Root.findViewById(R.id.send_email).setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.multchat_result_back:
		{
			this.getActivity().finish();

			break;
		}
		case R.id.send_email:
		{
			MultChatActivity mca = (MultChatActivity) this.getActivity();
			mca.goToNextPager(2);

			break;
		}
		default:
			break;
		}

	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{

		switch (taskID)
		{
		case TaskType.TRANS:
		{
			this.mpd.hide();
			String res = (String) paras[0];

			res = res.substring(1, res.length() - 1);

			StringBuffer sb = new StringBuffer("");
			for (String temp : res.split("\\$"))
			{
				sb.append(temp + "\r\n");
			}

			if (res != null)
			{
				this.resultTextView.setText(sb.toString());

				// 将文件写入到本地的sd卡中
				try
				{
					FileOutputStream fos = new FileOutputStream(new File(
							Sound.DIR + mRoomName + ".mrf"));
					fos.write(res.replace("$", " ").getBytes());
					fos.flush();
					fos.close();
				}
				catch (FileNotFoundException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// 将文件写入数据库
				mDbHelper.insertMultResultFile(qunzuName, mRoomName + ".mrf");
				MultChatActivity mca = (MultChatActivity) this.getActivity();
				mca.setmMultResult(res);

			}
			else
			{
				Toast.makeText(this.getActivity(), "转换出现问题……",
						Toast.LENGTH_LONG).show();
			}
			break;
		}

		default:
			break;
		}
		return null;

	}

	@Override
	public void onDestroy()
	{
		this.mpd.dismiss();
		MainService.removeActivity(MultChatResultFragement.class.getName());
		super.onDestroy();
	}

}
